package com.iteaj.iot.test;

import com.iteaj.iot.client.proxy.ProxyClientComponent;
import com.iteaj.iot.config.ConnectProperties;
import com.iteaj.iot.test.client.fixed.FixedLengthClientComponent;
import com.iteaj.iot.test.client.fixed.FixedLengthRequestHandle;
import com.iteaj.iot.test.client.line.LineClientComponent;
import com.iteaj.iot.test.client.line.LineClientHandle;
import com.iteaj.iot.test.modbus.ModbusTestComponent;
import com.iteaj.iot.test.modbus.ModbusTestHandle;
import com.iteaj.iot.test.mqtt.MqttClientTestComponent;
import com.iteaj.iot.test.mqtt.MqttClientTestHandle;
import com.iteaj.iot.test.proxy.ProxyClientCase;
import com.iteaj.iot.test.server.fixed.TestFixedLengthDecoderComponent;
import com.iteaj.iot.test.server.line.ServerLineBasedHandle;
import com.iteaj.iot.test.server.line.TestLineBasedFrameDecoderComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * create time: 2021/9/4
 *
 * @author iteaj
 * @since 1.0
 */
@EnableConfigurationProperties(IotTestProperties.class)
public class IotTestAutoConfiguration {


    @Autowired
    private IotTestProperties properties;

    @Bean
    @ConditionalOnExpression("${iot.test.line.port:null}!=null and ${iot.test.server:false} != false")
    public TestLineBasedFrameDecoderComponent lineBasedFrameDecoderComponent() {
        return new TestLineBasedFrameDecoderComponent(properties.getLine());
    }

    @Bean
    @ConditionalOnBean(TestLineBasedFrameDecoderComponent.class)
    public ServerLineBasedHandle serverLineBasedHandle() {
        return new ServerLineBasedHandle();
    }

    @Bean
    @ConditionalOnExpression("${iot.test.client} == true and ${iot.test.line.port:null}!=null")
    public LineClientComponent lineClientComponent() {
        IotTestProperties.TestConnectConfig line = properties.getLine();
        return new LineClientComponent(new ConnectProperties(line.getHost(), line.getPort()));
    }

    @Bean
    @ConditionalOnBean(LineClientComponent.class)
    public LineClientHandle lineClientHandle() {
        return new LineClientHandle();
    }

    /**
     * 固定长度解码器服务端测试
     * @return
     */
    @Bean
    @ConditionalOnExpression("${iot.test.fixed.port:null}!=null and ${iot.test.server:false} != false")
    public TestFixedLengthDecoderComponent lengthClientComponent() {
        return new TestFixedLengthDecoderComponent(properties.getFixed());
    }

    /**
     * 固定长度解码器客户端测试
     * @return
     */
    @Bean
    @ConditionalOnExpression("${iot.test.client} == true and ${iot.test.fixed.port:null}!=null")
    public FixedLengthClientComponent fixedLengthClientComponent() {
        return new FixedLengthClientComponent(properties.getFixed());
    }

    @Bean
    @ConditionalOnBean(FixedLengthClientComponent.class)
    public FixedLengthRequestHandle fixedLengthRequestHandle() {
        return new FixedLengthRequestHandle();
    }

    /**
     * mqtt协议客户端测试组件
     * @return
     */
    @Bean
    @ConditionalOnExpression("${iot.test.client} == true and ${iot.test.mqtt.port:null}!=null")
    public MqttClientTestComponent mqttClientTestComponent() {
        return new MqttClientTestComponent(properties.getMqtt());
    }

    @Bean
    @ConditionalOnBean(MqttClientTestComponent.class)
    public MqttClientTestHandle mqttClientTestHandle() {
        return new MqttClientTestHandle();
    }

    /**
     * modbus 协议测试
     * @return
     */
    @Bean
    @ConditionalOnExpression("${iot.test.client} == true and ${iot.test.modbus.port:null}!=null")
    public ModbusTestComponent modbusTestComponent() {
        return new ModbusTestComponent(properties.getModbus());
    }

    @Bean
    @ConditionalOnBean(ModbusTestComponent.class)
    public ModbusTestHandle modbusTestHandle() {
        return new ModbusTestHandle();
    }

    @Bean
    @ConditionalOnBean(ProxyClientComponent.class)
    public ProxyClientCase proxyClientCase() {
        return new ProxyClientCase();
    }
}
